package com.mccree.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class NettyByteBuf02 {
    public static void main(String[] args) {
        //创建bytebuf
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello World!", StandardCharsets.UTF_8);
        if (byteBuf.hasArray()){
            byte[] content = byteBuf.array();

            //将content转成字符串
            System.out.println(new String(content, StandardCharsets.UTF_8));

            System.out.println("byteBuf:"+byteBuf);


            System.out.println(byteBuf.arrayOffset());
            System.out.println(byteBuf.readerIndex());
            System.out.println(byteBuf.writerIndex());
            System.out.println(byteBuf.capacity());

            int len = byteBuf.readableBytes();
            System.out.println("len:"+len);

            //使用for取出各个字节
            for (int i = 0; i < len; i++) {
                System.out.print(((char) byteBuf.getByte(i))+"\t");
            }

            //按照某个范围读取
            System.out.println(byteBuf.getCharSequence(0,4,StandardCharsets.UTF_8));
            System.out.println(byteBuf.getCharSequence(4,6,StandardCharsets.UTF_8));
        }
    }
}
